using SmartCommunity.Models;
using Mapster;
using System.ComponentModel.DataAnnotations;

namespace SmartCommunity.WebApi.Services.System
{
    #region DTO属性映射
    /// <summary>
    /// 属性映射 RoleDtoRegister
    /// </summary>
    public class RoleDtoRegister : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<RoleEntity, RoleDto>().Map(d => d.MenuIds, s => s.Menus.Select(m => m.MenuId), s => s.Menus != null);
        }
    }
    #endregion

    #region 数据传输对象 RoleDto
    /// <summary>
    /// 数据传输对象：角色
    /// </summary>
    public class RoleDto : BaseDto
    {
        /// <summary>
        /// 角色ID
        /// </summary>
        public int RoleId { get; set; }

        /// <summary>
        /// 角色编码
        /// </summary>
        [Required(ErrorMessage = "角色编码不能为空")]
        [StringLength(50, ErrorMessage = "角色编码 长度不能超过 50.")]
        public string RoleCode { get; set; } = string.Empty;

        /// <summary>
        /// 角色名称
        /// </summary>
        [Required(ErrorMessage = "角色名称不能为空")]
        [StringLength(50, ErrorMessage = "角色名称 长度不能超过 50.")]
        public string RoleName { get; set; } = string.Empty;

        /// <summary>
        /// 备注说明
        /// </summary>
        [StringLength(255, ErrorMessage = "备注说明 长度不能超过 255.")]
        public string? Remark { get; set; }

        /// <summary>
        /// 是否作废
        /// </summary>
        public bool Disabled { get; set; }

        /// <summary>
        /// 关联的菜单ID
        /// </summary>
        public IEnumerable<int> MenuIds { get; set; } = new List<int>();

    }
    #endregion

    #region 数据查询对象
    /// <summary>
    /// 查询模型：角色
    /// </summary>
    public class RoleQuery : AbstractQuery<RoleEntity>
    {
        /// <summary>
        /// 主键ID多选查询
        /// </summary>
        public IEnumerable<int> Ids { get; set; } = [];

        /// <summary>
        /// 是否作废
        /// </summary>
        public bool? Disabled { get; set; }

        /// <summary>
        /// 根据查询条件添加查询表达式
        /// </summary>
        protected override void MergeQueryExpression()
        {
            AddExpression(Ids.Any(), e => Ids.Contains(e.RoleId));
            AddExpression(Disabled.HasValue, r => r.Disabled == Disabled!.Value);
            AddSearchExpression(s => o => o.RoleCode.Contains(s) || o.RoleName.Contains(s));
            AddDateExpression(start => e => e.CreatedTime >= start, end => e => e.CreatedTime <= end);
        }
    }
    #endregion
}
